package com.yxq.mdt.app.modules.order.ui.adapter;

import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.view.MotionEvent;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.yxq.mdt.app.R;
import com.yxq.mdt.app.dialog.CostumerDialog;
import com.yxq.mdt.app.modules.order.bean.EventOrderMsg;
import com.yxq.mdt.app.modules.order.bean.OrderListBean;
import com.yxq.mdt.app.modules.order.model.OrderModel;
import com.yxq.mdt.app.modules.order.model.OrderStatusUtils;
import com.yxq.mdt.app.modules.order.ui.OrderDetailActivity;
import com.yxq.mdt.app.modules.order.ui.OrderRefundDetailActivity;
import com.yxq.mdt.app.net.ApiModel;

import org.greenrobot.eventbus.EventBus;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import static com.blankj.utilcode.util.ActivityUtils.startActivity;

/**
 * @author L
 * @time 2019-11-01
 * @describe 我的订单售后订单列表$
 */
public class OrderAfterSaleListAdapter extends BaseQuickAdapter<OrderListBean.ListBean, BaseViewHolder> {
    Timer timer = null;
    float startX = 0;
    float startY = 0;
    private static boolean isLongClickModule = false;

    public OrderAfterSaleListAdapter(int layoutResId, @Nullable List<OrderListBean.ListBean> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, OrderListBean.ListBean item) {
        String status = OrderStatusUtils.refundStatus( item.getIs_agree(), item.getIs_user_send(), item.getRefund_status());
        helper.setText(R.id.order_tv_date, item.getAddtime());
        helper.setText(R.id.order_tv_status, "订单编号：" + item.getOrder_no());

        helper.setText(R.id.order_tv_return_price, String.format("￥%.2f", item.getRefund_price()));
        helper.setText(R.id.order_tv_price, status);

        RecyclerView recyclerView = helper.getView(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        OrderAfterSaleListGoodsListAdapter orderListGoodsListAdapter = new OrderAfterSaleListGoodsListAdapter(R.layout.layout_order_return_list_goods_item_layout, item.getGoods_list());
        recyclerView.setAdapter(orderListGoodsListAdapter);

        recyclerView.setOnTouchListener((view, motionEvent) -> {
            switch (motionEvent.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    startX = motionEvent.getX();
                    startY = motionEvent.getY();
                    timer = new Timer();
                    timer.schedule(new TimerTask() {
                        @Override
                        public void run() {
                            isLongClickModule = true;
                            new Handler(Looper.getMainLooper()).post(new Runnable() {
                                @Override
                                public void run() {
                                    if (status.equals("已同意并退款") || status.equals("已同意换货")|| status.equals("已拒绝退换货")) {

                                        //添加你长按之后的方法
                                        CostumerDialog dialog = new CostumerDialog.Builder(mContext, R.style.custom_dialog_no_match)
                                                .setContentView(R.layout.dialog_clear_cache_layout)
                                                .setCancelable(true)
                                                .create();

                                        TextView title = dialog.findViewById(R.id.tv_title);

                                        title.setText("提示");

                                        TextView content = dialog.findViewById(R.id.tv_content);
                                        content.setText("确定删除订单？");

                                        //继续
                                        TextView sure = dialog.findViewById(R.id.tv_sure);
                                        sure.setText("确定");
                                        sure.setOnClickListener(v2 -> {
                                            dialog.dismiss();
                                            OrderModel.delete(item.getOrder_id(), new ApiModel.CommonCallback<String>() {
                                                @Override
                                                public void onSuccess(String s) {
                                                    ToastUtils.showShort("删除成功");
                                                    EventBus.getDefault().post(new EventOrderMsg(5));
                                                }

                                                @Override
                                                public void onError(String errMsg) {
                                                    ToastUtils.showShort(errMsg);
                                                }
                                            });
                                        });

                                        //取消
                                        TextView cancel = dialog.findViewById(R.id.tv_cancel);
                                        cancel.setText("取消");
                                        cancel.setOnClickListener(v1 -> {
                                            dialog.dismiss();
                                        });

                                        dialog.show();
                                    }
                                }
                            });
                            timer.cancel();
                            timer = null;
                        }
                    }, 800); // 按下时长设置
                    break;
                case MotionEvent.ACTION_MOVE:
                    double deltaX = Math.sqrt((motionEvent.getX() - startX) * (motionEvent.getX() - startX) + (motionEvent.getY() - startY) * (motionEvent.getY() - startY));
                    if (deltaX > 20 && timer != null) { // 移动大于20像素
                        timer.cancel();
                        timer = null;
                    }
                    if (isLongClickModule) {

                    }
                    break;
                default:
                    if (!isLongClickModule) {
                        //订单详情
                        Intent intent = new Intent(mContext, OrderRefundDetailActivity.class);
                        intent.putExtra("orderId", item.getOrder_refund_id());
                        startActivity(intent);
                    }
                    isLongClickModule = false;
                    if (timer != null) {
                        timer.cancel();
                        timer = null;
                    }

            }
            return true;
        });
    }
}
